import numpy as np

def sliding_window_k_quantile(matrix, window_width=16, k=0.5):
    """
    对输入矩阵按行进行滑动窗口 K 分位数计算
    
    参数:
        matrix: 输入矩阵，形状为 (rows, cols)
        window_width: 滑动窗口大小 width
        k: 分位数参数 (0 <= k <= 1)
    
    返回:
        结果矩阵，形状为 (rows, cols - window_width + 1)
    """
    rows, cols = matrix.shape
    result_cols = cols - window_width + 1
    result = np.zeros((rows, result_cols))
    
    for i in range(rows):
        for j in range(result_cols):
            window = matrix[i, j:j+window_width]
            result[i, j] = np.quantile(window, k)
    
    return result

# 示例使用
# 生成一个 1024x2048 的随机矩阵
np.random.seed(42)
matrix = np.random.rand(1024, 1024)

# 计算滑动窗口的中位数 (k=0.5)
result = sliding_window_k_quantile(matrix, window_width=16, k=0.5)

print("Input:", matrix, matrix.shape)
print("Output:", result, result.shape)